<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('client.batch_transfer') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('client.name')" prop="client_id">
            <el-select
              v-model="dataForm.client_id"
              :placeholder="i18n('common.please_select') + i18n('client.name')" collapse-tags multiple clearable>
              <el-option v-for="(v, k) in clientList" :label="v.name" :key="k" :value="v.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item :label="i18n('contact.salesperson')" prop="user_id">
            <el-select
              v-model="dataForm.user_id"
              :placeholder="i18n('common.please_select') + i18n('contact.salesperson')" clearable>
              <el-option v-for="(v, k) in userList" :label="v.nickname" :key="k" :value="v.id"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="client-transfer">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/client')

  const userList = ref([])
  const clientList = ref([])

  const dataForm = reactive({
    user_id: '',
    client_id: [],
  })

  const dataRule = reactive({
    user_id: [{
      required: true,
      message: i18n('client.rules.user_id.require'),
      trigger: 'blur',
    }],
    client_id: [{
      required: true,
      message: i18n('client.rules.client_id.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = () => {
    $http({
      url: $http.adornUrl(`/${model.value}/select`),
      method: 'get',
      params: $http.adornParams({
        user_id: 0
      }),
    }).then(({ data }) => {
      if (data && data.data && data.status === 200) {
        clientList.value = data.data
      }
    })
  }


  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/batch_transfer`),
          method: 'post',
          data: $http.adornData({
            client_id: dataForm.client_id,
            user_id: dataForm.user_id,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const loadUserList = () => {
    $http({
      url: $http.adornUrl(`/user/select`),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        userList.value = data.data
      }
    })
  }


  onMounted(() => {
    init(formRef)

    loadUserList()
  })
</script>
